#coding:utf-8
import runner
import sys
import data

def run():
    """
    @type ok: okocjk
    @param wait: wair ok
    @todo: 
        - asdj
        - jhksdf
        - dsfjkhasd
    """
    r = runner.HtmlTestRunner("test.db", "example.html")
    result = r.load_and_run("", ["."])
    return result

def render():
    from mako.template import Template as Mako_Template
    from mako.runtime import Context as Mako_Context
    session = data.Session(bind=data.get_engine('test.db'))
    tc_list = session.query(data.TestCase).all()
    tlist, tp = [], {}
    for tc in tc_list:
        mod = tc.path.rsplit('.', 1)[0]
        if not tp.has_key(mod):
            d = {'cases': [], 'fail':0, 'pass':0, 'name': mod}
            tlist.append(d)
            tp[mod] = d
        mk = tp[mod]
        mk['cases'].append(tc)
        for result in tc.results:
            if result.result is False:
                mk['fail'] += 1
                break
        else:
            mk['pass'] += 1
    t = Mako_Template(filename='test_result.html')
    f = file("example.html", 'w')
    c = Mako_Context(f, results=tlist, description={'time start': '1', 'time stop':'2', 'title': 'no title', 'status': '2'})
    t.render_context(c)
    f.close()
    
run()